//-----------------------------------------------------------------------
// <copyright file="BannerSizeDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class BannerSizeDAOBase
    {
        #region Common methods
        public virtual BannerSize CreateBannerSizeFromReader(IDataReader reader)
        {
            BannerSize item = new BannerSize();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SizeId"))) item.SizeId = (long)reader["SizeId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("WidthSize"))) item.WidthSize = (long)reader["WidthSize"];
                if (!reader.IsDBNull(reader.GetOrdinal("HightSize"))) item.HightSize = (long)reader["HightSize"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartusId"))) item.StartusId = (int)reader["StartusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerSizeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateBannerSize methods
            
        public virtual void CreateBannerSize(BannerSize bannerSize)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerSizeInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, bannerSize.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, bannerSize.Description);
                database.AddInParameter(dbCommand, "@WidthSize", DbType.Int64, bannerSize.WidthSize);
                database.AddInParameter(dbCommand, "@HightSize", DbType.Int64, bannerSize.HightSize);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, bannerSize.Order);
                database.AddInParameter(dbCommand, "@StartusId", DbType.Int32, bannerSize.StartusId);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, bannerSize.Enable);
                database.AddOutParameter(dbCommand, "@SizeId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                bannerSize.SizeId = (long)database.GetParameterValue(dbCommand, "@SizeId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerSizeException, ex);
            }
        }

        #endregion

        #region UpdateBannerSize methods
        
        public virtual void UpdateBannerSize(BannerSize bannerSize)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerSizeUpdate");            
                
                database.AddInParameter(dbCommand, "@SizeId", DbType.Int64, bannerSize.SizeId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, bannerSize.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, bannerSize.Description);
                database.AddInParameter(dbCommand, "@WidthSize", DbType.Int64, bannerSize.WidthSize);
                database.AddInParameter(dbCommand, "@HightSize", DbType.Int64, bannerSize.HightSize);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, bannerSize.Order);
                database.AddInParameter(dbCommand, "@StartusId", DbType.Int32, bannerSize.StartusId);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, bannerSize.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateBannerSizeException, ex);
            }
        }
        
        #endregion

        #region DeleteBannerSize methods
        public virtual void DeleteBannerSize(long sizeId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerSizeDelete");
                
                database.AddInParameter(dbCommand, "@SizeId", DbType.Int64, sizeId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteBannerSizeException, ex);
            }
        }

        #endregion

        #region GetBannerSize methods
        
        public virtual BannerSize GetBannerSize(long sizeId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerSizeGet");
                
                database.AddInParameter(dbCommand, "@SizeId", DbType.Int64, sizeId);
                
                BannerSize bannerSize =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        bannerSize = CreateBannerSizeFromReader(reader);
                        reader.Close();
                    }
                }
                return bannerSize;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerSizeException, ex);
            }
        }
        
        #endregion

        #region GetBannerSizeList methods
        public virtual BannerSizeCollection GetBannerSizeList(BannerSizeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerSizeGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerSizeCollection bannerSizeCollection = new BannerSizeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        BannerSize bannerSize = CreateBannerSizeFromReader(reader);
                        bannerSizeCollection.Add(bannerSize);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerSizeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerSizeListException, ex);
            }
        }
        
        public virtual BannerSizeCollection GetBannerSizeList(BannerSizeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetBannerSizeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetBannerSizeList methods
        public virtual BannerSizeCollection GetBannerSizeListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerSizeGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerSizeCollection bannerSizeCollection = new BannerSizeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        BannerSize bannerSize = CreateBannerSizeFromReader(reader);
                        bannerSizeCollection.Add(bannerSize);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerSizeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerSizeListException, ex);
            }
        }
        
        public virtual BannerSizeCollection GetBannerSizeListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetBannerSizeListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}